<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/3/31
 * Time: 上午11:29
 */

namespace App\Logic;


class BaseLogic
{
    /**
     * 成功后返回数据
     * @param null $data
     * @param null $msg
     * @param int $code
     * @return array
     */
    protected static function success($data = null, $code = 200, $msg = null)
    {
        return self::result($code, $msg, $data);
    }

    /**
     * 失败后返回数据
     * @param string $msg 失败后返回msg
     * @param int $code 默认403
     * @return array
     */
    protected static function error($msg, $code = 400)
    {
        return self::result($code, $msg);
    }

    /**
     * 返回约定数据
     *
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return array
     */
    private static function result($code = 200, $msg = '', $data = [])
    {
        if(empty($data)){
            $data = json_decode('{}');
        }
        $resultJson = ['code' => $code, 'msg' => $msg, 'data' => $data];
        exit(response()->json($resultJson)->send());
    }

    public static function p($data = array(), $type = null)
    {
        header('Content-Type:text/html;charset=utf-8');
        echo '<pre>';
        print_r($data);
        echo '</pre>';
        !$type ? exit () : null;
    }
}